//
//  DVWeatherDataModel.m
//  QuickCure
//
//  Created by 松鼠君 on 9/13/16.
//  Copyright © 2016 haoge. All rights reserved.
//

#import "DVWeatherDataModel.h"
#import "DVWeatherDataDetailData.h"

@implementation DVWeatherDataModel


- (instancetype)initWithCityName:(NSString*)CityName andBlock:(void(^)(BOOL dayType))block
{
    self = [super init];
    if (self) {
        
        [self fecthDataWithCityName:CityName andBlock:block];
        
    }
    return self;
}


-(void)setWeather_data:(NSArray<DVWeatherDataDetailData*> *)weather_data{
    
    _weather_data = weather_data;
    
    NSMutableArray* arr = [NSMutableArray array];
    
    [weather_data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        DVWeatherDataDetailData* weatherDataDetailData = [DVWeatherDataDetailData new];
        
        [weatherDataDetailData setValuesForKeysWithDictionary:obj];
        
        [arr addObject: weatherDataDetailData];
        
    }];
    
    _weather_data = arr.copy;
    
    
    
}

-(void)fecthDataWithCityName:(NSString*)CityName andBlock:(void(^)(BOOL dayType))block{
    
    
    NSString *city = CityName;
    NSString *url = @"http://api.map.baidu.com/telematics/v3/weather";
    
    NSDictionary *paramDic = @{@"location":city,
                               @"output":@"json",
                               @"ak":@"17IvzuqmKrK1cGwNL6VQebF9"
                               };
    
    
    AFHTTPSessionManager* manager = [ AFHTTPSessionManager manager];
    
    [manager GET:url parameters:paramDic progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
        NSDictionary* dic = responseObject;
        
        NSArray* arr = dic[@"results"] ;
        
        NSDictionary* tmp = arr[0];

        [self setValuesForKeysWithDictionary:tmp];
        
        //判断时间
        
        NSDate* date = [NSDate date];
        
        NSDateFormatter* formatter = [NSDateFormatter new];
        
        formatter.dateFormat = @"HH";
        
        NSString* dateStr = [formatter stringFromDate:date];

        
        BOOL dayType = dateStr.integerValue >6 && dateStr.integerValue <18 ?1:0;

        
        
        //        NSLog(@"%@",self);


        
        if (block) {
            
            block(dayType);

        }

        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"error = %@",error);
        
    }];
    
    
    
    
}



- (NSString *)description
{
    return [NSString stringWithFormat:@"self.currentcity = %@", self.currentCity];
}

@end
